package com.yuong.echart;

import android.util.Log;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * 〈玫瑰图数据工具〉
 *
 * @author 袁栋
 * @create 2021/5/29
 * @since 1.0.0
 */
public class EChartUtil {
    public List<DataBean> dataList = new ArrayList<>();

    public EChartUtil() {
        initData();
    }

    private void initData() {
        dataList.add(new DataBean("视频广告", 600.5));
        dataList.add(new DataBean("联盟广告", 274));
        dataList.add(new DataBean("邮件营销", 310));
        dataList.add(new DataBean("直接访问", 335));
        dataList.add(new DataBean("搜索引擎", 400));
    }

    public String getRoseOption() {
        String[] colors = {"#FFF200", "#FF0000", "#00A2E8", "#6BD089", "#C785C8"};
        RoseOption option = new RoseOption();
        List<RoseOption.SeriesBean> series = new ArrayList<>();
        List<RoseOption.PieBean> pies = new ArrayList<>();
        List<String> legendTitles = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            DataBean temp = dataList.get(i);
            //图例
            legendTitles.add(temp.getName());

            //数据
            RoseOption.PieBean pie = new RoseOption.PieBean();
            pie.setName(temp.getName());
            pie.setValue(temp.getValue());

            RoseOption.PieBean.ItemStyle itemStyle = new RoseOption.PieBean.ItemStyle();
            itemStyle.setColor(colors[i]);
            pie.setItemStyle(itemStyle);

            pies.add(pie);
        }

        option.getLegend().setData(legendTitles);

        RoseOption.SeriesBean seriesBean = new RoseOption.SeriesBean();
        seriesBean.setData(pies);

        series.add(seriesBean);

        option.setSeries(series);
        String json = new Gson().toJson(option);
        Log.e("*****************", json);
        return json;
    }
}